#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
分析排列5训练数据分布
检查是否存在数据不平衡导致模型总预测0
"""

import pandas as pd
import numpy as np
import os
from collections import Counter

def analyze_plw_data():
    """分析排列5数据分布"""
    print(" 分析排列5训练数据分布")
    print("=" * 60)
    
    data_file = "./scripts/plw/plw_history.csv"
    if not os.path.exists(data_file):
        print(f"[ERROR] 数据文件不存在: {data_file}")
        return False
    
    # 读取数据
    data = pd.read_csv(data_file)
    print(f" 数据形状: {data.shape}")
    print(f" 数据列: {data.columns.tolist()}")
    print(f" 前5行数据:")
    print(data.head())
    
    # 分析draw_numbers列的格式
    if 'draw_numbers' in data.columns:
        print(f"\n draw_numbers列分析:")
        print(f"前10个draw_numbers:")
        for i, numbers in enumerate(data['draw_numbers'].head(10)):
            print(f"  {i+1}: {numbers}")
        
        # 解析数字并分析分布
        all_numbers = []
        for numbers_str in data['draw_numbers']:
            if isinstance(numbers_str, str):
                # 去除引号并按逗号分割
                numbers = numbers_str.strip('"').split(',')
                # 取前5个数字
                if len(numbers) >= 5:
                    for i in range(5):
                        try:
                            num = int(numbers[i].strip())
                            all_numbers.append(num)
                        except ValueError:
                            print(f"[WARNING] 无法解析数字: {numbers[i]}")
        
        print(f"\n 解析到的数字总数: {len(all_numbers)}")
        
        # 分析数字分布
        counter = Counter(all_numbers)
        print(f"\n 数字分布:")
        for digit in range(10):
            count = counter.get(digit, 0)
            percentage = (count / len(all_numbers)) * 100 if all_numbers else 0
            print(f"  数字 {digit}: {count} 次 ({percentage:.2f}%)")
        
        # 检查是否存在严重的数据不平衡
        max_count = max(counter.values()) if counter else 0
        min_count = min(counter.values()) if counter else 0
        imbalance_ratio = max_count / min_count if min_count > 0 else float('inf')
        
        print(f"\n 数据平衡性分析:")
        print(f"  最大出现次数: {max_count}")
        print(f"  最小出现次数: {min_count}")
        print(f"  不平衡比例: {imbalance_ratio:.2f}")
        
        if imbalance_ratio > 3:
            print(f"[WARNING] 检测到严重的数据不平衡！这可能导致模型偏向高频数字")
        
        # 分析0的出现频率
        zero_count = counter.get(0, 0)
        zero_percentage = (zero_count / len(all_numbers)) * 100 if all_numbers else 0
        print(f"\n 特别关注数字0:")
        print(f"  数字0出现次数: {zero_count}")
        print(f"  数字0出现比例: {zero_percentage:.2f}%")
        
        if zero_percentage > 15:
            print(f"[WARNING] 数字0出现频率过高！这可能是模型总预测0的原因")
        
        # 检查数据范围
        unique_numbers = set(all_numbers)
        print(f"\n 数据范围检查:")
        print(f"  唯一数字: {sorted(unique_numbers)}")
        print(f"  数据范围: {min(all_numbers)} - {max(all_numbers)}")
        
        # 检查是否有超出0-9范围的数字
        out_of_range = [num for num in all_numbers if num < 0 or num > 9]
        if out_of_range:
            print(f"[WARNING] 发现超出0-9范围的数字: {set(out_of_range)}")
            print(f"   数量: {len(out_of_range)}")
        
        return True
    else:
        print("[ERROR] 未找到draw_numbers列")
        return False

if __name__ == "__main__":
    analyze_plw_data()